<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				sponsorCacheKey:'zyk-hlpy-suggestionReview-sponsor-cache',
				instCacheKey:'zyk-hlpy-suggestionReview-inst-cache',
				salaryPosId:''
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','orgId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			onSelectImplementationUser(item){
				let _that = this
				_that.salaryPosId = item.salaryPosId
				_that.$refs.autoform.clear("title6","posId")
				_that.om.request(
					'orgInstPosUserOfficial/getPosComboboxByUser',
					{
						userId:item.id
					}
				).then(res=>{
					for(let pos of res){
						if(pos.id == _that.salaryPosId){
							_that.$refs.autoform.formdata.posId = pos.id
							_that.$refs.autoform.formdata.posName = pos.posName
						}
					}
					_that.$refs.autoform.changeEditor("title6","posId","data",configSelectData(res,'id','postName'))
					_that.$refs.autoform.initSelectIndex("title6","posId")
				})
			},
			implementationUserType(){
				let _that = this
				_that.$refs.autoform.changeEditor("title6","implementationUserId",'data',[])
				_that.$refs.autoform.changeEditor("title6","implementationUserId",'type','selectbox')
			},
			changPosType(){
				let _that = this
				_that.$refs.autoform.changeEditor("title6","posId",'data',[])
				_that.$refs.autoform.changeEditor("title6","posId",'type','selectbox')
			},
			findUserList(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/findUserList",
					{
						instId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title6","implementationUserId",'data',configSelectData(res,'id','username'))
					_that.$refs.autoform.changeEditor("title6","implementationUserId","onSelect","onSelectImplementationUser")
					_that.$refs.autoform.initSelectIndex("title6","implementationUserId")
				})
			},
			getPrimaryData(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPrimaryData",
					{
						instId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.organizationUserId = res.value.userId
					_that.$refs.autoform.formdata.organizationUser = res.value.userName
				})
			},
			changeResponsibleOrganizationType(){
				let _that = this
				_that.$refs.autoform.changeEditor("title6","responsibleOrganizationId",'type','linkbox')
				_that.$refs.autoform.changeEditor("title6","responsibleOrganizationId",{
					link: '../../public/search/search',
					"linkParam": {
						other: JSON.stringify({
							orgId: _that.orgId
						}),
						url: "tinstitution/getComboTreeNotBord",
						namefield: "text",
						initload: true,
						cachename: _that.instCacheKey,
						placeholder: "请输入部门名称"
					}
				})
			},
			onSelectReviewComment(item){
				let _that = this
				if(item.code=='39316'){
					_that.changeResponsibleOrganizationType()
					_that.changPosType()
					_that.implementationUserType()
					_that.$refs.autoform.changeEditor("title6","completionDate","type","newdatebox")
					_that.$refs.autoform.changeEditor("title6","deliverables","type","textarea")
				}else{
					_that.$refs.autoform.clear("title6","responsibleOrganizationId")
					_that.$refs.autoform.clear("title6","organizationUserId")
					_that.$refs.autoform.clear("title6","implementationUserId")
					_that.$refs.autoform.clear("title6","posId")
					_that.$refs.autoform.clear("title6","completionDate")
					_that.$refs.autoform.clear("title6","deliverables")
					_that.$refs.autoform.changeEditor("title6","responsibleOrganizationId","type","labelbox")
					_that.$refs.autoform.changeEditor("title6","organizationUserId","type","labelbox")
					_that.$refs.autoform.changeEditor("title6","implementationUserId","type","labelbox")
					_that.$refs.autoform.changeEditor("title6","posId","type","labelbox")
					_that.$refs.autoform.changeEditor("title6","completionDate","type","labelbox")
					_that.$refs.autoform.changeEditor("title6","deliverables","type","labelbox")
				}
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:'tsysdict/getCombobox',
						param:{
							code:'39315'
						}
					}
				]
				if(fileId){
					ajaxArr.push(
						{
							url:'suggestionReview/getValue',
							param:{
								fileId:fileId
							}
						},
						{
							url:'suggestionReview/getSuggestionReviewDTO',
						}
					)
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posName"),mainData)
					changeEditor("title6","reviewCommentId","data",configSelectData(res[1],"id","dicname"),mainData)
					changeEditor("title1","sponsorCode",{
						link:'../../public/pageSearch/pageSearch',
						linkParam: {
							other: JSON.stringify({
								
							}),
							initload: true,
							namefield:'sponsorCode',
							notefield :'proposalName',
							searchfield:'q',
							url:'saneProposalList/getDataGrid',
							cachename: _that.sponsorCacheKey,
						}
					},null,mainData)
					
					let tempFileList = []
					let fileUrlList = []
					changeEditor('bottominfo','bottomFileUpload','fileUploadKey',"74",mainData)
					changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
					changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
					
					if(fileId){
						var fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[2].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value[index])
								_that.$set(_that.formdata,index,res[2].value[index])
							}
						}
						if(_that.formdata.bottomFileList){
							let tempFileList = []
							let fileUrlList = _that.formdata.bottomFileList.map(t=>{
								tempFileList.push(t.fileSaveName)
								return t.fileSaveName
							})
							changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
							changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
						}
						if(_that.$refs.autoform.formdata.reviewCommentId && _that.$refs.autoform.formdata.reviewCommentId == '5489'){
							changeEditor("title6","responsibleOrganizationId",'type','linkbox',mainData)
							changeEditor("title6","responsibleOrganizationId",{
								link: '../../public/search/search',
								"linkParam": {
									other: JSON.stringify({
										orgId: _that.orgId
									}),
									url: "tinstitution/getComboTreeNotBord",
									namefield: "text",
									initload: true,
									cachename: _that.instCacheKey,
									placeholder: "请输入部门名称"
								}
							},null,mainData)
							changeEditor("title6","implementationUserId",'data',[],mainData)
							changeEditor("title6","implementationUserId",'type','selectbox',mainData)
							changeEditor("title6","posId",'data',[],mainData)
							changeEditor("title6","posId",'type','selectbox',mainData)
						}
						if(_that.$refs.autoform.formdata.responsibleOrganizationId){
							changeEditor("title6","implementationUserId",'data',configSelectData(res,'id','username'),mainData)
							changeEditor("title6","implementationUserId","onSelect","onSelectImplementationUser",mainData)
						}
						if(res[3].value.caiNaCode == '39316'){
							changeEditor("title6","responsibleOrganizationId",'type','linkbox',mainData)
							changeEditor("title6","responsibleOrganizationId",{
								link: '../../public/search/search',
								"linkParam": {
									other: JSON.stringify({
										orgId: _that.orgId
									}),
									url: "tinstitution/getComboTreeNotBord",
									namefield: "text",
									initload: true,
									cachename: _that.instCacheKey,
									placeholder: "请输入部门名称"
								}
							},null,mainData)
							changeEditor("title6","posId",'data',[],mainData)
							changeEditor("title6","posId",'type','selectbox',mainData)
							changeEditor("title6","implementationUserId",'data',[],mainData)
							changeEditor("title6","implementationUserId",'type','selectbox',mainData)
							if(_that.$refs.autoform.formdata.responsibleOrganizationId){
								_that.om.request(
									"orgInstPosUserOfficial/findUserList",
									{
										instId:_that.$refs.autoform.formdata.responsibleOrganizationId
									}
								).then(res=>{
									_that.$refs.autoform.changeEditor("title6","implementationUserId",'data',configSelectData(res,'id','username'))
									_that.$refs.autoform.changeEditor("title6","implementationUserId","onSelect","onSelectImplementationUser")
									_that.$refs.autoform.initSelectIndex("title6","implementationUserId")
								})
							}
							if(_that.$refs.autoform.formdata.implementationUserId){
								_that.om.request(
									'orgInstPosUserOfficial/getPosComboboxByUser',
									{
										userId:_that.$refs.autoform.formdata.implementationUserId
									}
								).then(res=>{
									_that.$refs.autoform.changeEditor("title6","posId","data",configSelectData(res,'id','postName'))
									_that.$refs.autoform.initSelectIndex("title6","posId")
								})
							}
						}
					}
					_that.$nextTick(function(){
						_that.$refs.autoform.initSelectIndex("title6","reviewCommentId")
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("title6","implementationUserId")
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				_that.formdata.updateBottomFileFlag= true
				if(Object.prototype.toString.call(_that.$refs.autoform.formdata.bottomFileList) == "[object String]"){
					_that.formdata.bottomFileListStr = _that.$refs.autoform.formdata.bottomFileList|| ''
				}else{
					_that.formdata.bottomFileListStr = JSON.stringify(_that.$refs.autoform.formdata.bottomFileList || []) || ''
				}
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				if(!_that.edit){
					_that.om.formSave('suggestionReview/doAdd',_that.formdata,_that)
				}else{
					_that.om.formSave('suggestionReview/doUpdate',_that.formdata,_that)
				}
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				};
				uni.getStorage({
					key:_that.sponsorCacheKey,
					success:(res)=>{
						if(res.data){
							let data = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.sponsorCode = data.sponsorCode
							formdata.ptime = data.ptime
							formdata.sponsorId = data.sponsorId
							formdata.sponsorName = data.sponsorName
							formdata.postId = data.postId
							formdata.postName = data.postName
							formdata.organizationId = data.organizationId
							formdata.organizationName = data.organizationName
							formdata.phone = data.phone
							formdata.proposalCategoryId = data.proposalCategoryId
							formdata.proposalCategoryName = data.proposalCategoryName
							formdata.proposalName = data.proposalName
							formdata.expect = data.expect
							formdata.problem = data.problem
							formdata.estimatedCost = data.estimatedCost
							formdata.proposalContent = data.proposalContent
							uni.removeStorage({
								key:_that.sponsorCacheKey
							})
						}
					}
				});
				uni.getStorage({
					key:_that.instCacheKey,
					success: (res) => {
						if(res.data){
							let d = res.data
							let formdata  = _that.$refs.autoform.formdata
							formdata.responsibleOrganizationId = d.id
							formdata.responsibleOrganization = d.text
							_that.$refs.autoform.clear("title6","posId")
							_that.$refs.autoform.clear("title6","implementationUserId")
							_that.getPrimaryData(d.id)
							_that.findUserList(d.id)
							uni.removeStorage({
								key:_that.instCacheKey
							})
						}
					}
				})
				
			}
		}
	}
</script>

<style>
</style>
       